package com.example.broadcast;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    MyReceiver receiver1;
    //网络
    private IntentFilter filter2;
    private NetworkChangedReceiver receiver2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        receiver1=new MyReceiver();//创建广播接收器对象
        IntentFilter filter1=new IntentFilter();//创建过滤器对象
        filter1.addAction(Intent.ACTION_SCREEN_ON); //添加要监听的广播 Action
        filter1.addAction(Intent.ACTION_SCREEN_OFF); //添加要监听的广播 Action
        registerReceiver(receiver1, filter1); //注册广播





        filter2 = new IntentFilter();
        //为过滤器添加处理规则
        filter2.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        receiver2 = new NetworkChangedReceiver();
        //注册广播接收器
        registerReceiver(receiver2, filter2);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver1);//注销广播
        unregisterReceiver(receiver2);//注销广播
    }

}
